<template>
	<view class="content">
		<headnav title='主页' :showMenu='false' :bgc="'#f1f1f1'"></headnav>
		<loading v-if="isloading"></loading>
		<container v-else>
			<!-- 此处有插槽，可以直接把组件当父标签 -->
			<scroll-view scroll-y="true" style="width: 100%;height: 100%;">
				<view class="loginBox">
					<!-- 当前为静态 -->
					<image :src="headsrc" @tap="tomine()"></image>
					<text class="username" @tap="tomine()">{{nickname}}</text>
					<view class="btn" @tap='quit'>退出登录</view>
				</view>
				<view class="searchBox" @tap="toSearch()">
					<text class="iconfont icon-fangdajing"></text>
					<text>搜索歌曲</text>
				</view>

				<!-- <view class="skeleton" v-if="isloading">
					<m-for-skeleton :avatarSize="200" :row="3" :loading="isloading" :isarc="'square'" v-for="key in 4" :key="key"
					 :title="false">
					</m-for-skeleton>
				</view> -->
				<view>
					<view class="each" v-for="(n,i) in list" :key="i" @tap="toList(n.id)">
						<view class="imgbox">
							<image :src="n.coverImgUrl"></image>
							<text>{{n.updateFrequency}}</text>
						</view>
						<view class="textbox">
							<!-- h5 提示 key最好使用字符串或者数字 不要使用复杂类型 -->
							<view v-for="(j,k) in n.tracks" :key="k">
								<text> {{k+1}}.{{j.first}}-{{j.second}}</text>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</container>
	</view>
</template>

<script>
	import headnav from '@/components/headNav/headNav.vue'
	import container from '@/components/container/container.vue'
	import {
		topList
	} from '../../common/api.js'
	export default {
		data() {
			return {
				title: 'Hello',
				list: [],
				isloading: true,
				headsrc: '../../static/headPic.jpg',
				nickname: '当前未登录'
			}
		},
		onShow() {
			//encodeURIComponent()
			//刷新用户信息
			if (uni.getStorageSync('userinfo')) {
				this.setuserinfo()
			}
		},
		onLoad() {
			if (uni.getStorageSync('userinfo')) {
				this.setuserinfo()
			}
			// 因为是加载完毕周期 所以后期需要用到骨架屏
			topList().then(res => {
				this.isloading = false
				this.list = res.data.list.slice(0, 4)
				console.log(this.list)
			})
		},
		methods: {
			setuserinfo() {
				let rs = JSON.parse(uni.getStorageSync('userinfo'))
				this.headsrc = rs.avatarUrl
				this.nickname = rs.nickname
			},
			tomine() {
				uni.switchTab({
					url: "/pages/mine/mine"
				})
			},
			quit() {
				uni.showModal({
					title: '提示',
					content: '确认退出登录?',
					success: function(res) {
						if (res.confirm) {
							uni.removeStorageSync('token')
							uni.removeStorageSync('userinfo')
							uni.navigateTo({
								url: '/pages/login/login'
							});
						} else if (res.cancel) {

						}
					}
				})
			},
			toList(id) {
				uni.navigateTo({
					url: '/pages/list/list?id=' + id
				});
			},
			toSearch() {
				uni.navigateTo({
					url: '/pages/search/search',
				});
			}
		},
		components: {
			headnav,
			container,
		}
	}
</script>

<style scoped lang="scss">
	.loginBox {
		width: 90%;
		height: 90rpx;
		margin: 0 auto;
		display: flex;
		align-items: center;
		position: relative;

		image {
			border-radius: 50%;
			width: 70rpx;
			height: 70rpx;
		}

		.username {
			font-size: 28rpx;
			margin: 0 10rpx;
		}

		.btn {
			position: absolute;
			right: 0;
			border-radius: 15rpx;
			font-size: 25rpx;
			padding: 4rpx 15rpx;
			border: 1px solid rgb(190, 190, 190);
		}
	}

	.banner {
		width: 90%;
		height: 300rpx;
		display: block;
		margin: 20rpx auto;
		border-radius: 30rpx;
	}

	.searchBox {
		width: 90%;
		height: 70rpx;
		margin: 60rpx auto 0;
		background: #f7f7f7;
		border-radius: 30rpx;
		display: flex;
		align-items: center;

		.iconfont {
			font-size: 26rpx;
			margin: 0 28rpx;
		}

		text {
			font-size: 28rpx;
		}

	}

	.each {
		height: 212rpx;
		width: 90%;
		margin: 40rpx auto;
		display: flex;
		position: relative;

		.imgbox {
			height: 212rpx;
			width: 212rpx;
			border-radius: 30rpx;
			overflow: hidden;
			margin-right: 30rpx;

			image {
				width: 100%;
				height: 100%;
			}

			text {
				position: absolute;
				left: 16rpx;
				bottom: 12rpx;
				color: white;
				font-size: 22rpx;
			}
		}

		.textbox {
			position: relative;
			width: calc(100% - 242rpx);
			font-size: 20rpx;
			line-height: 70.66rpx;

			text {
				font-size: 30rpx;
				display: block;
				width: 95%;
				text-overflow: ellipsis;
				white-space: nowrap;
				overflow: hidden;
			}
		}
	}
</style>
